package lc2;

public class Ex470 {

    class SolBase {
        public int rand7() {
            return 0;
        }
    }

    class Solution extends SolBase {
        public int rand10() {
            //[0，48]的随机数
            int cur = 7 * (rand7() - 1) + rand7() - 1;
            if (cur < 40) {
                return cur % 10 + 1;
            } else {
                //[0, 8]的随机数
                int sub = cur - 40;
                //[0, 62]的随机数
                cur = sub * (rand7() - 1) + rand7() - 1;
                if (cur < 60) {
                    return cur % 10 + 1;
                } else {
                    //[0,2]的随机数
                    sub = cur - 60;
                    //[0, 21]的随机数
                    cur = sub * (rand7() - 1) + rand7() - 1;
                    if (cur < 20) {
                        return cur % 10 + 1;
                    } else {
                        return rand10();
                    }
                }
            }
        }
    }
}
